package com.hhq.sso.auth.base.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hhq.sso.common.api.CommonResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <p>
 * Description: 自定义用户权限不足的处理
 * </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/6/20 11:37
 */
@Component
public class AuthAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        String value = new ObjectMapper().writeValueAsString(CommonResult.fail("权限不足！"));
        response.getWriter().write(value);
    }
}
